<template>
  <div class="xtx-form">
    <div class="user-info">
      <img :src="nickname" alt="" />
      <p>Hi，{{avatar}} 欢迎来小兔鲜，完成绑定后可以QQ账号一键登录哦~</p>
    </div>
    <Form
    ref="target"
    :validation-schema="mySchema"
    v-slot="{ errors }"
    autocomplete="off"
    class="xtx-form"
  >
     <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <Field
          :class="{ err: errors.mobile }"
          v-model="form.mobile"
          name="mobile"
          class="input"
          type="text"
          placeholder="绑定的手机号"
        />
      </div>
      <div v-if="errors.mobile" class="error">{{ errors.mobile }}</div>
    </div>
     <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <Field
          :class="{ err: errors.code }"
          v-model="form.code"
          name="code"
          class="input"
          type="text"
          placeholder="短信验证码"
        />
        <span  class="code" @click="send">
            {{countTime === 0? '发送验证码' : `${countTime}s之后发送`}}
        </span>
      </div>
      <div v-if="errors.code" class="error">{{ errors.code }}</div>
    </div>
    <a href="javascript:;" class="submit" @click="submit">立即绑定</a>
     </Form>
  </div>

</template>

<script>
import QC from 'qc'
import { ref, reactive } from 'vue'
import schema from '@/utils/vee-validate-schema'
import { Form, Field } from 'vee-validate'
import { useCountDown } from '@/compositions/index'
import { userQQBindCode, userQQBindLogin } from '@/api/user'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
export default {
  name: 'CallbackBind',
  props: {
    unionId: {
      type: String
    }
  },
  components: {
    Form, Field
  },
  setup (props) {
    const nickname = ref('')
    const avatar = ref('')
    QC.api('get_user_info').success((res) => {
      nickname.value = res.data.figureurl_qq_1
      avatar.value = res.data.nickname
    })

    // 表单数据对象
    const form = reactive({
      mobile: '',
      code: ''
    })
    // 校验规则对象
    const mySchema = {
      mobile: schema.mobile,
      code: schema.code
    }
    // 点击验证码进行校验手机号和发送验证码
    const { countTime, start } = useCountDown(60)
    function send () {
      const valid = mySchema.mobile(form.mobile)
      if (valid === true) {
        if (countTime.value === 0) {
          userQQBindCode(form.mobile).then(res => {
            start()
          })
        }
      }
    }

    const target = ref(null)
    const store = useStore()
    const router = useRouter()
    // 兜底验证
    async function submit () {
      const ref = await target.value.validate()
      if (ref) {
        const res = await userQQBindLogin({
          unionId: props.unionId,
          mobile: form.mobile,
          code: form.code
        })
        // 保存数据到vuex中
        store.commit('user/setUser', res)
        // 跳转路由
        router.push('/')
      }
    }
    return {
      nickname,
      avatar,
      form,
      mySchema,
      send,
      countTime,
      target,
      submit
    }
  }
}
</script>

<style scoped lang='less'>
.user-info {
    width: 320px;
    height: 70px;
    margin: 0 auto;
    display: flex;
    background: #f2f2f2;
    align-items: center;
    padding: 0 10px;
    margin-bottom: 25px;
    img {
      background: #f2f2f2;
      width: 50px;
      height: 50px;
    }
    p {
      padding-left: 10px;
    }
  }
  .code {
    position: absolute;
    right: 0;
    top: 0;
    line-height: 50px;
    width: 80px;
    color: #999;
    &:hover {
      cursor: pointer;
    }
  }
</style>
